// var WEB_HOST = "http://www.pk.com";
var WEB_HOST = '/';


$(document).ready(function(){

// initfooter();

try{
  // new WOW().init();
}catch(e){
  console.log('WOW error');
}

// 分页加载功能
var PAGEAJAX={};
    PAGEAJAX.el = {
        'list':$('#list'),
        'loading':$('.listloading'),
        'bottom':$('.listbottom')

    };
    PAGEAJAX.page = 2;
    PAGEAJAX.isajax = true;
    PAGEAJAX.json = {"p":PAGEAJAX.page++};
    
function getdata (PAGEAJAX){
    if(!PAGEAJAX.isajax ||PAGEAJAX.el.list.data('url')==undefined||PAGEAJAX.el.list.data('url').length==0) return;
    PAGEAJAX.isajax = false;
    PAGEAJAX.el.loading.removeClass('dn');
    $.xposthtml( PAGEAJAX.el.list.data('url'), PAGEAJAX.json, function(code, html) {
        // console.log(code+html);
        if (code == 0) {
            PAGEAJAX.el.loading.addClass('dn');
            if (html.trim().length>0) {
                PAGEAJAX.el.list.append(html);
                 PAGEAJAX.isajax = true;
            }else{
                PAGEAJAX.el.loading.addClass('dn');
                PAGEAJAX.el.bottom.removeClass('dn');
            }
        } else {
            if(PAGEAJAX.page > 2){
                PAGEAJAX.el.loading.addClass('dn');
                PAGEAJAX.el.bottom.removeClass('dn');
            }else{
                PAGEAJAX.el.loading.text('还没有相关信息哟...');
            }
        }
    })
}


$(window).scroll(function () {
    if (scrollToBottom()) {
        // console.log(JSON.stringify(PAGEAJAX));
        getdata(PAGEAJAX);
    }
});



// 收藏功能
$('.fav').on('click',function(){
    var that = $(this);
    var posturl = that.parent().data('posturl');
    var favtype = that.parent().data('favtype');
    var id = that.parent().data('id');
    var status = that.hasClass('ion-ios-heart')?'0':'1';

    var json = {
        status:status
    }
    json[favtype] = id; 

    $.post(posturl,json,function(result){
        var result = JSON.parse(result);
        if(!result.errcode){
            layer.msg(result.errmsg,{"time":1000});
        }else{
            if(that.hasClass('ion-ios-heart')){
                that.removeClass('ion-ios-heart').removeClass('_active_primary').addClass('ion-ios-heart-outline');
            }else{
                that.removeClass('ion-ios-heart-outline').addClass('ion-ios-heart').addClass('_active_primary');
            }
            layer.msg(result.errmsg,{"time":1000});
        }
    })

})





$('body').on('click','.gopage',function(){

    var url = $(this).data('url');
    if( url==undefined||url.length==0){
        return;
    }
    window.location.href = url;
})



//回到顶部
$('.floaticon.top').click(function(){
    $("html, body").animate({scrollTop: 0}, 500);
    return false;
});


// 弹层滚动穿透
document.getElementById('cover')&&document.getElementById('cover').addEventListener('touchmove', function(e) {
  e.preventDefault();
}, false);
document.getElementById('filterpanel')&&document.getElementById('filterpanel').addEventListener('touchmove', function(e) {
  e.preventDefault();
}, false);


//页面滑动特效
var SCROLL={};
    SCROLL.cHeight = 0;
    SCROLL.shadowHeight = 20;
    SCROLL.el={
        'fixheader':$('.fixedshadow'),
        'nav':$('.nav'),
        'top':$('.floaticon.top')
    }

$(window).scroll(function(){

    if ($(this).scrollTop()>SCROLL.cHeight){
        // if(!SCROLL.el.fixheader.hasClass('y') && SCROLL.cHeight>300) SCROLL.el.fixheader.css("transform","translateY(-4rem)").addClass('y');
        if(!SCROLL.el.top.hasClass('y') && SCROLL.cHeight>300) SCROLL.el.top.css("transform","translateY(-150px)").addClass('y');

    } else {
        // if(SCROLL.el.fixheader.hasClass('y') || SCROLL.cHeight<300) SCROLL.el.fixheader.css("transform","translateY(0px)").removeClass('y');
        if(SCROLL.el.top.hasClass('y') || SCROLL.cHeight<300) SCROLL.el.top.css("transform","translateY(0px)").removeClass('y');

    }

    if(SCROLL.cHeight>SCROLL.shadowHeight) {
           if(!SCROLL.el.fixheader.hasClass('showshadow'))  SCROLL.el.fixheader.addClass('showshadow');   
           if(!SCROLL.el.nav.hasClass('showshadow'))  SCROLL.el.nav.addClass('showshadow');   
    }else{
           if(SCROLL.el.fixheader.hasClass('showshadow')) SCROLL.el.fixheader.removeClass('showshadow');  
           if(SCROLL.el.nav.hasClass('showshadow')) SCROLL.el.nav.removeClass('showshadow');  
    }

    SCROLL.cHeight = $(this).scrollTop();

});






// if($(".footer .f-item").length!=5){//不是首页5个板块
//     var gohome = '<div class="homeandback gohome"><a class="animated fadeInRight" href="/" title="返回控制台"><i class="fa fa-home"></i></a></div>';
//     $("body").append(gohome);

//     var goback = '<div class="homeandback goback"><a class="animated fadeInLeft" href="javascript:void(0)" title="返回"><i class="fa fa-angle-left"></i></a></div>';
//     $("body").append(goback);

//     $('.wrap').css('margin-bottom','1rem');

// }


// setTimeout(function(){
//     hidepreloading();
//     $('.wrap').removeClass('dn').addClass('fadeIn animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
//           $(this).removeClass('fadeIn animated');
//         });

// },500)


})

// 获取浏览器窗口的可视区域的宽度
function getViewPortWidth() {
    return document.documentElement.clientWidth || document.body.clientWidth;
}

// 获取浏览器窗口的可视区域的高度
function getViewPortHeight() {
    return document.documentElement.clientHeight || document.body.clientHeight;
}

// 获取浏览器窗口水平滚动条的位置
function getScrollLeft() {
    return document.documentElement.scrollLeft || document.body.scrollLeft;
}

// 获取浏览器窗口垂直滚动条的位置
function getScrollTop() {
    return document.documentElement.scrollTop || document.body.scrollTop;
}

//  获取文档的高度
function getDocumentHeight() {
    return document.documentElement.scrollHeight || document.body.scrollHeight;
}

//判断是否滚动到底部  一般在windwo.scroll监听
function scrollToBottom() {
    return getDocumentHeight()<= parseFloat(getViewPortHeight()) + parseFloat(getScrollTop());
}




/***
 *设置cookie
 * @param json json对象
 * @param time 过期时间 <0 表示删除
 * @returns {boolean}
 */
function setCookie(json,time){
    if(navigator.cookieEnabled){
        if(typeof(time)== undefined) time = 3600;
        for(var p in json){
            $.cookie(p,json[p],time);
        }
        return true;
    }
    return false;
}
function getCookie(key){
    if(navigator.cookieEnabled){
        return $.cookie(key);
    }
    return '';
}


/*
hide loading when data is loaded;
 */
var hidepreloading = function(){
    $('.preloading,.loader-color').removeClass().addClass('fadeOut animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
      $(this).remove();
    });
}


var initfooter = function(){

    if($(document.body).height()<$(window).height()){
    $('.footer').css('width','100%');
    $('.footer').css('position','absolute');
    $('.footer').css('bottom','0px');
    // $('.footer').css('left','0px');
    // $('.wrap').css('padding-bottom','55px');
    }
}